/*
 * @Date: 2023-04-14 09:12:58
 * @LastEditors: Dannycjh 2233879049@qq.com
 * @LastEditTime: 2023-04-18 13:10:17
 */
import { Director, director, sys } from "cc";
import { Singleton,EventConst } from "../common/Common";
import { App } from "../App";
import { GAME_RES_LIST } from "../game/billiards/GameManage";
import { LoadMg } from "./LoadMg";

/**
 * 场景管理类
 */
export default class SceneNavigator extends Singleton<SceneNavigator> {
    /** 首页场景名称 */
    public get home() { return this._home; }
    private _home: string = "home";

    /** 历史记录（栈） */
    public get history() { return this._history; }
    private _history: string[] = [];

    /** 当前场景名称 */
    public get curScene() { return this._curScene; }
    private _curScene: string = null;

    /** 上一个场景留下的参数（ onLaunched 之后可用） */
    public get param() { return this._param; }
    private _param: any = null;

    /** 预加载游戏场景资源 */
    public resList = {}

    /** 预加载大厅场景资源 */
    public hallResList = {}

    /** 游戏场景名称 */
    public gameNames = ["game1","game2","game3","game4","game5","home","login","multiplay","multiroom","bloodselect"]

    /**
     * 设置首页
     * @param name 场景名
     */
    public setHome(name: string) {
        this._home = name;
        this._history = [name];
        this._curScene = name;
    }

    /**
     * 返回首页
     * @param param 数据
     * @param coverHistory 覆盖历史记录
     * @param onLaunched 场景加载完成回调
     */
    public goHome(name: string, param?: any, onLaunched?: Function) {
        this._param = null;
        if (this._curScene === name) {
            return;
        }
        
        this.loadHallResList(() => {
            director.loadScene(name, () => {
                // if (coverHistory) {
                //     this._history.length = 0;
                // }
                
                if (this._history.indexOf(name) == -1) {
                    this._history.push(name);
                }
                
                this._curScene = name;
                this._param = param || null;
                onLaunched && onLaunched();

                sys.garbageCollect()
            });
        })
    }

    /**
     * 前往场景
     * @param name 场景名
     * @param param 数据
     * @param onLaunched 场景加载完成回调
     */
    public go(name: string, param?: any, onLaunched?: Function) {
        if (this._curScene == name) {
            return
        }

        LoadMg.showLoad({type:1})
        this._param = null;
        this.loadGameResList(() => {
            director.loadScene(name, (err, scene) => {
                if (this._history.indexOf(name) == -1) {
                    this._history.push(name);
                }
                
                this._curScene = name;
                this._param = param || null;
                onLaunched && onLaunched(err, scene);
            });
       })
    }
    /**
     * 返回上一个场景
     * @param param 数据
     * @param onLaunched 场景加载完成回调
     */
    public back(param?: any, onLaunched?: Function) {
        if (this._history.length < 2) {
            return;
        }
        this._param = null;
        const history = this._history,
        name = history[history.length - 2];
        director.loadScene(name, () => {
            history.pop();
            this._curScene = name;
            this._param = param || null;
            onLaunched && onLaunched();
        });
    }

    /**
     * 加载游戏场景资源
     */
    public loadGameResList(cb) {
        let count = Object.keys(this.resList).length
        if (count>0) { // 已经加载过了
            cb && cb()
            return
        }

        App.LoadMg.loadResList(GAME_RES_LIST, (resList) => {
            this.resList = resList
            cb && cb()
       })
    }

    // 获取游戏资源
    public getRes(path) {
        if (!this.resList[path]) {
            console.error("资源不存在", path)
            return null
        }
        
        return this.resList[path]
    }

    /**
     * 加载大厅场景资源
     */
    public loadHallResList(cb) {
        let count = Object.keys(this.hallResList).length
        if (count>0) { // 已经加载过了
            cb && cb()
            return
        }

        App.LoadMg.loadResList(App.HALL_RES_LIST, (hallResList) => {
            this.hallResList = hallResList
            cb && cb()
       })
    }

    // 获取大厅资源
    public getHallRes(path) {
        if (!this.hallResList[path]) {
            console.error("资源不存在", path)
            return null
        }
        
        return this.hallResList[path]
    }
}